package com.leagueoflegends.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leagueoflegends.bean.Champion;
import com.leagueoflegends.bean.ChampionPassive;
import com.leagueoflegends.bean.ChampionSkin;
import com.leagueoflegends.bean.ChampionSpell;
import com.leagueoflegends.service.ChampionPassiveService;
import com.leagueoflegends.service.ChampionService;
import com.leagueoflegends.service.ChampionSkinService;
import com.leagueoflegends.service.ChampionSpellService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 英雄管理页面
 */


@CrossOrigin
//@Controller
@RestController
@RequestMapping("/champion")
public class ChampionController {

    @Autowired
    ChampionService championService;
    @Autowired
    ChampionSpellService championSpellService;
    @Autowired
    ChampionSkinService championSkinService;
    @Autowired
    ChampionPassiveService championPassiveService;

    //查询所有英雄
    @GetMapping
    public PageInfo<Champion> list(
            @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "12") int pageSize,
            @RequestParam(value = "type", defaultValue = "all") String type,
            @RequestParam(value = "find", defaultValue = "") String find
    ) {
        List<Champion> list = championService.selectAll(pageNum, pageSize, type, find);
        return PageInfo.of(list);
    }

    // 新增英雄
    @PostMapping("/insertChampion")
    public String insertChampion(@RequestBody Champion champion) {
        System.out.println(champion);
        championService.insert(champion);
        return "";
    }

    // 根据 id 逻辑删除英雄
    @DeleteMapping("/deleteChampionById/{id}")
    public String deleteChampionById(@PathVariable("id") Integer id) {
        championService.delete(id);
        return "";
    }

    // 根据 id 修改英雄
    // 接对象要用PostMapping 不能用PutMapping
    @PostMapping("/updateChampionById")
    public String updateChampionById(@RequestBody Champion champion) {
        championService.update(champion);
        return "";
    }

    // 前台用户页面根据 id 查询英雄
    @GetMapping("/sel-id")
    public Champion selectById(Integer id) {
        Champion champion = championService.selectById(id);
        champion.setPassive(championPassiveService.select(champion.getId()));
        champion.setSpells(championSpellService.select(champion.getId()));
        champion.setSkins(championSkinService.select(champion.getId()));
        return champion;
    }

    // 后台管理根据id查询单个英雄详细信息
    @GetMapping("/selectChampionById")
    public Champion selectChampionById(@RequestParam(value = "id", defaultValue = "0") int id){
        Champion champion = championService.selectById(id);
        return champion;
    }

    // 恢复已删除英雄
    @PutMapping("/regainChampion/{id}")
    public String regainChampion(@PathVariable("id") int id){
        championService.regainChampion(id);
        return "";
    }



    // 前台查询所有英雄
    @ResponseBody
    @RequestMapping("/getAllChampion")
    public List<Champion> getAllChampion() {
        List<Champion> list = new ArrayList<>();
        list = championService.getAllChampion();
        System.out.println(list);
        return list;
    }

    // 前台根据职业查询英雄
    @ResponseBody
    @RequestMapping(value = "/sel-job/{job}", method = RequestMethod.GET)
    public List<Champion> selectByJob(@PathVariable String job) {
        System.out.println(job);
        List<Champion> list = new ArrayList<>();
        list = championService.selectByJob(job);
        System.out.println(list);
        return list;
    }

    // 前台模糊查询
    @ResponseBody
    @RequestMapping("/getChampionByTitle")
    public List<Champion> getChampionByTitle(@Param("title") String title) {
        System.out.println(title);
//        String t = "'%"+title+"%'";
        List<Champion> list = championService.getChampionByTitle(title);
        System.out.println(list);
        return list;
    }


}
